package cn.iocoder.yudao.module.member.controller.app.shunfengcheorder;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.shunfengcheorder.vo.ShunfengcheOrderCreateReqVO;
import cn.iocoder.yudao.module.member.controller.admin.shunfengcheorder.vo.ShunfengcheOrderPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.shunfengcheorder.vo.ShunfengcheOrderRespVO;
import cn.iocoder.yudao.module.member.convert.shunfengcheorder.ShunfengcheOrderConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.shunfengcheorder.ShunfengcheOrderDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.mpshunfengcheorderevaluate.MpShunfengcheOrderEvaluateService;
import cn.iocoder.yudao.module.member.service.shunfengcheorder.ShunfengcheOrderService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Api(tags = "用户 APP - 公众号顺风车订单")
@RestController
@RequestMapping("/member/mp/shunfengche-order")
@Validated
public class AppShunfengcheOrderController {

    @Resource
    private ShunfengcheOrderService shunfengcheOrderService;

    @Resource
    private MpShunfengcheOrderEvaluateService evaluateService;


    @Resource
    private MemberUserService userService;

    @PostMapping("/create")
    @ApiOperation("创建公众号顺风车订单")
    public CommonResult<Long> createShunfengcheOrder(@Valid @RequestBody ShunfengcheOrderCreateReqVO createReqVO) {
        MemberUserDO user = userService.getUser(getLoginUserId());
        createReqVO.setSource(1);
        createReqVO.setPersonPhone(user.getMobile());
        return success(shunfengcheOrderService.createShunfengcheOrder(createReqVO));
    }


    @GetMapping("/get")
    @ApiOperation("获得公众号顺风车订单")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<ShunfengcheOrderRespVO> getShunfengcheOrder(@RequestParam("id") Long id) {
        ShunfengcheOrderDO shunfengcheOrder = shunfengcheOrderService.getShunfengcheOrder(id);
        ShunfengcheOrderRespVO convert = ShunfengcheOrderConvert.INSTANCE.convert(shunfengcheOrder);
        if(Objects.nonNull(convert)){
            convert.setEvaluate(evaluateService.getEvaluate(id));
        }
        return success(convert);
    }

    @GetMapping("/cancel")
    @ApiOperation("取消公众号顺风车订单")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<Boolean> cancelOrder(@RequestParam("id") Long id) {
        return success(shunfengcheOrderService.changeOrderStatus(id, 3));
    }

    @GetMapping("/receive")
    @ApiOperation("司机接单")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<Boolean> receive(@RequestParam("id") Long id) {
        return success(shunfengcheOrderService.changeOrderStatus(id, 1));
    }

    @GetMapping("/page")
    @ApiOperation("获得公众号顺风车订单分页")
    public CommonResult<PageResult<ShunfengcheOrderRespVO>> getShunfengcheOrderPage(@Valid ShunfengcheOrderPageReqVO pageVO) {
//        pageVO.setCreator(getLoginUserId().toString());
        PageResult<ShunfengcheOrderDO> pageResult = shunfengcheOrderService.getShunfengcheOrderPage(pageVO);
        return success(ShunfengcheOrderConvert.INSTANCE.convertPage(pageResult));
    }


}
